/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.knolle.vistas;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import org.marcelo.agro.entidades.Lotes;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;

public class CampaniasWizardPanel2 implements WizardDescriptor.Panel<WizardDescriptor>,PropertyChangeListener {

    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private CampaniasVisualPanel2 component;
    private WizardDescriptor model;
    private Boolean isValid = false;
    private final EventListenerList listeners = new EventListenerList();

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    @Override
    public CampaniasVisualPanel2 getComponent() {
        if (component == null) {
            component = new CampaniasVisualPanel2();
        }
        return component;
    }

    @Override
    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx("help.key.here");
    }

    @Override
    public boolean isValid() {
        // If it is always OK to press Next or Finish, then:
        return isValid;
        // If it depends on some condition (form filled out...) and
        // this condition changes (last form field filled in...) then
        // use ChangeSupport to implement add/removeChangeListener below.
        // WizardDescriptor.ERROR/WARNING/INFORMATION_MESSAGE will also be useful.
    }

    @Override
    public void addChangeListener(ChangeListener l) {
        listeners.add(ChangeListener.class, l);
    }

    @Override
    public void removeChangeListener(ChangeListener l) {
        listeners.remove(ChangeListener.class, l);
    }

    @Override
    public void readSettings(WizardDescriptor wiz) {
        // use wiz.getProperty to retrieve previous panel state
        model = wiz;
        getComponent().addPropertyChangeListener(this);
    }

    @Override
    public void storeSettings(WizardDescriptor wiz) {
        // use wiz.putProperty to remember current panel state
        CampaniasVisualPanel2 panel2 =  getComponent();
        int max = panel2.getLotesSeleccionados().getModel().getSize();
        if (max > 0){
            Lotes[] lotesSeleccionados = new Lotes[max];
            for(int i=0; i <max ;i++) {
                lotesSeleccionados[i] = (Lotes) panel2.getLotesSeleccionados().getModel().getElementAt(i);
            }
            wiz.putProperty("lotesSeleccionados", lotesSeleccionados);
        }
        
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
       Boolean oldValue = isValid;
       isValid = checkValidez();
       fireChangeEvent(this,oldValue,isValid);
    }
    
    private Boolean checkValidez(){
        if (getComponent().getLotesSeleccionados().getModel().getSize() > 0 ){
           isValid = true;
        }
        else {
           isValid = false;
        }
        return isValid;
    }
    
    protected final void fireChangeEvent(
        Object source, boolean oldState, boolean newState) {
            if(oldState != newState) {
                ChangeEvent ev = new ChangeEvent(source);
                    for (ChangeListener listener : listeners.getListeners(ChangeListener.class)) {
                        listener.stateChanged(ev);
                    }
            }
    }
}
